package com.mall.order.services;

import com.mall.order.StockQueryService;
import com.mall.order.dal.entitys.Stock;
import com.mall.order.dal.persistence.StockMapper;
import com.mall.order.dto.StockQueryDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 岳子昂
 * @version 1.0
 * @description:
 * @date 2022-01-24 20:17
 */
@Service
public class StockQueryServiceImpl implements StockQueryService {

    @Autowired
    StockMapper stockMapper;

    /**
     * @description:
     * @param:
     * @return:
     * @author 岳子昂
     * @date: 2022-01-24 20:17
     */
    @Override
    public StockQueryDto stockQuery(Long itemId) {

        Example example = new Example(Stock.class);
        example.createCriteria().andEqualTo("itemId", itemId);
        List<Stock> stocks = stockMapper.selectByExample(example);
        Long stockCount = null;
        // 若没有找到相应的库存值，那么说明该商品没有库存，返回0
        if (stocks == null || stocks.size() == 0) {
            stockCount = 0l;
        } else {
            Stock stock = stocks.get(0);
            stockCount = stock.getStockCount();
        }

        StockQueryDto stockQueryDto = new StockQueryDto();
        stockQueryDto.setLimitNum(stockCount);

        return stockQueryDto;
    }
}
